<template>
  <div class="pagination">
    <button  @click="$emit('currentChange',currentPage - 1)" :disabled="currentPage==1">上一页</button>

    <button v-show="startAndEndNumber.start > 1" @click="$emit('currentChange',1)">1</button>
    <button v-show="startAndEndNumber.start > 2">...</button>
    <!-- 中间连续五个数字展示 -->
    <!-- 
       v-for:数组、对象、数字、字符串....
       面试题:v-for可不可以与v-if一起使用(可以
       v-for与v-if优先级:v-for
     -->
    <button v-for="(page,index) in startAndEndNumber.end" :key="index" v-if="page>=startAndEndNumber.start" @click="$emit('currentChange',page)" :class="{cur:currentPage==page}">{{page}}</button>
    


    <button v-show="startAndEndNumber.end < totalPage - 1 ">...</button>
    <button v-show="startAndEndNumber.end < totalPage" @click="$emit('currentChange',totalPage)">{{totalPage}}</button>
    <button @click="$emit('currentChange',currentPage + 1)" :disabled="currentPage==totalPage">下一页</button>
    
    <button style="margin-left: 30px">共 {{total}} 条</button>
  </div>
</template>

<script>
  export default {
    name: "Pagination",
    props:['total','pageSize','currentPage','pagerCount'],
    //计算属性
    computed:{
      //分页器一共多少页-计算出来(向上取证)
      totalPage(){
          return Math.ceil(this.total/this.pageSize);
      },
      //计算出连续页码起始数字与结束数字
      startAndEndNumber(){
        //准备两个变量：存储起始与结束数字
        const {currentPage,pagerCount,totalPage} = this;
        //声明两个变量:连续页码起始数字与结束数字
        let start = 0, end  =  0;
        //开始
        start = currentPage - parseInt(pagerCount/2);
        //结束
        end = currentPage  +  parseInt(pagerCount/2);
        //如果只是按照上面算法：计算连续页码的起始与结束的数字，可能出现错误的情况【负数|零】
        //顶上这种可能出现错误情况-左边 1 2 
        if(start < 1){
           //如果start这个数字出现负数或者零归1
           start = 1;
           //判断end结束的这个数字[默认分页器至少五页]
           if(totalPage > pagerCount){
             end = pagerCount;
           }else{
             end = totalPage;
           }
        }
        //顶上这种可能出现错误情况-右边 32 33 
        if(end >totalPage){
           start = totalPage - pagerCount + 1;
           //如果出现end非法：最大就是总页码
           end = totalPage;
        }
        return {start,end};
      }
    }
  }
</script>

<style lang="less" scoped>
  .pagination {
    button {
      margin: 0 5px;
      background-color: #f4f4f5;
      color: #606266;
      outline: none;
      border-radius: 2px;
      padding: 0 4px;
      vertical-align: top;
      display: inline-block;
      font-size: 13px;
      min-width: 35.5px;
      height: 28px;
      line-height: 28px;
      cursor: pointer;
      box-sizing: border-box;
      text-align: center;
      border: 0;

      &[disabled] {
        color: #c0c4cc;
        cursor: not-allowed;
      }

      &.active {
        cursor: not-allowed;
        background-color: #409eff;
        color: #fff;
      }
    }
      
    button.cur{
      background:yellowgreen;
      color:white;
      border-radius:2px 2px 2px 2px;
      transform:scale(1.1);
    }


  }
</style>